package View;

import Service.DormService;
import Service.ServiceImpl.DormServiceImpl;
import Service.ServiceImpl.StudentServiceImpl;
import Service.ServiceImpl.UserServiceImpl;
import Service.ServiceImpl.stustateServiceImpl;
import Service.StudentService;
import Service.UserService;
import Service.stustateService;
import domain.Dorm;
import domain.Student;
import domain.User;
import domain.stustate;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class regist extends JFrame {
    public static void main(String[] args) {
        new regist().setVisible(true);
    }

    public regist(){
        init();
        componentInit();
    }

    public void init(){
        this.setTitle("注册");
        this.setSize(466,406);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(){
        //------------------JLabel--------------------
        JLabel label6= new JLabel();
        label6.setText("请输入性别:");
        label6.setBounds(95,45,85,36);
        JLabel label5= new JLabel();
        label5.setText("请输入宿舍号:");
        label5.setBounds(95,5,85,36);
        JLabel label1= new JLabel();
        label1.setText("请输入用户名:");
        label1.setBounds(95,86,81,36);
        JLabel label2= new JLabel();
        label2.setText("请输入密码:");
        label2.setBounds(95,142,85,30);
        JLabel label3= new JLabel();
        label3.setText("请再次输入密码核对:");
        label3.setBounds(59,195,123,30);
        JLabel label4= new JLabel();
        label4.setText("请输入学生号:");
        label4.setBounds(95,230,85,36);
        JLabel label7= new JLabel();
        label7.setText("请输入班级:");
        label7.setBounds(95,280,85,36);


        //------------------JTextField--------------------
        JTextField field1= new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(189,86,145,32);
        JTextField field2= new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(190,230,140,32);
        DormService service = new DormServiceImpl();
        List<Dorm> dorms = service.dormList();
        Vector<String> item2= new Vector<>();
        item2.add("请选择你的宿舍");
        for (int i = 0; i < dorms.size(); i++) {
            Dorm dorm = dorms.get(i);
            item2.add(dorm.getDormnum());
        }

        JComboBox comboBox2= new JComboBox(item2);
        comboBox2.setBounds(190,5,140,32);

        String[] item = new String[3];
        item[0]="请选择性别";
        item[1]="男";
        item[2]="女";
        JComboBox comboBox1= new JComboBox(item);
        comboBox1.setBounds(190,45,140,32);

        JTextField field5= new JTextField();
        field5.setText("");
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(190,280,140,32);

        //------------------JPasswordField--------------------
        JPasswordField password1= new JPasswordField();
        password1.setBorder(new LineBorder(Color.lightGray));
        password1.setBounds(190,141,143,32);
        JPasswordField password2= new JPasswordField();
        password2.setBorder(new LineBorder(Color.lightGray));
        password2.setBounds(191,192,140,32);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("注册");
        button1.setBounds(79,330,95,27);
        JButton button2= new JButton();
        button2.setText("返回");
        button2.setBounds(277,330,95,27);





        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(label5);
        this.add(label6);
        this.add(label7);
        this.add(field1);
        this.add(field2);
        this.add(comboBox2);
        this.add(comboBox1);
        this.add(field5);
        this.add(button1);
        this.add(button2);
        this.add(password1);
        this.add(password2);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = field1.getText();
                String stunum = field2.getText();
                String password = password1.getText();
                String checkPwd = password2.getText();

                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                user.setType("user"); //默认成为普通用户
                user.setStunum(stunum);

                Student student =  new Student();
                student.setStunum(field2.getText());
                student.setName(field1.getText());
                student.setSex((String) comboBox1.getSelectedItem());
                student.setClasses(field5.getText());
                student.setDormnum((String) comboBox2.getSelectedItem());



                stustate state = new stustate();
                state.setStunum(field2.getText());
                state.setName(field1.getText());
                state.setIsindorm("false");


                if(password.equals(checkPwd)){
                    UserService service = new UserServiceImpl();
                    service.addUser(user);

                    StudentService service1 = new StudentServiceImpl();
                    System.out.println(student);
                    service1.addStudent(student);


                    stustateService service2 = new stustateServiceImpl();
                    service2.addStudentState(state);

                    JOptionPane.showMessageDialog(new JFrame(),"注册成功!返回登录页面");
                    new Login().setVisible(true);
                    dispose();
                }

            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Login().setVisible(true);
            }
        });
    }
}

